package com.ceshi.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ceshi.dao.ProductDao;
import com.ceshi.dao.TransDao;
import com.ceshi.meta.Product;
import com.ceshi.meta.Trans;

@Controller
public class ProductAction {

	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String show(@RequestParam int id, HttpServletRequest req, ModelMap map) {
		// start session
		HttpSession session = req.getSession();
		String usertype = null;
		int userid = -1;

		if (session.getAttribute("username") != null) {
			usertype = (String) session.getAttribute("usertype");
			userid = (Integer) session.getAttribute("userid");
		}

		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		ProductDao productDao = context.getBean(ProductDao.class);

		Product product = productDao.getProductById(id);
		if (product == null) {
			return "show";
		}
		
		Map productmap = new HashMap();
		productmap.clear();
		productmap.put("id", product.getId());
		productmap.put("image", product.getIcon());
		productmap.put("title", product.getTitle());
		productmap.put("price", product.getPrice());
		productmap.put("summary", product.getSummary());
		productmap.put("detail", product.getText());
		boolean isBuy = false;
		boolean isSell = false;
		int buyNum = 0;

		TransDao transDao = context.getBean(TransDao.class);
		Trans tran = transDao.getTransbyContentIdAndUserid(product.getId(), userid);
		// 0 买家，1卖家
		if (tran != null) {
			if (usertype.equals("0")) {
				isBuy = true;
				productmap.put("buyPrice", tran.getPrice());
				buyNum = tran.getBuyNum();
			} else {
				isSell = true;
				productmap.put("saleNum", tran.getBuyNum());
			}
		}

		productmap.put("buyNum", buyNum);
		productmap.put("isBuy", isBuy);
		productmap.put("isSell", isSell);
		map.addAttribute("product", productmap);

		return "show";
	}

	@RequestMapping(value = "/public", method = RequestMethod.GET)
	public String publicapi(HttpServletRequest req, ModelMap map) {
		System.out.println("public:");
		return "public";
	}

	@RequestMapping(value = "/publicSubmit", method = RequestMethod.POST)
	public String publicsubmit(@RequestParam String title, @RequestParam String image, @RequestParam String detail,
			@RequestParam int price, @RequestParam String summary, HttpServletRequest req, ModelMap map) {

		System.out.println("publicSubmit:" + title);

		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		ProductDao productdao = context.getBean(ProductDao.class);

		Product product = new Product();
		product.setTitle(title);
		product.setPrice(price);
		product.setText(detail);
		product.setIcon(image);
		product.setSummary(summary);
		productdao.insertProduct(product);
		System.out.println("插入成功:" + product.getId());

		map.addAttribute("product", product);
		return "publicSubmit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(@RequestParam int id, HttpServletRequest req, ModelMap map) {
		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		ProductDao productDao = context.getBean(ProductDao.class);

		Product product = productDao.getProductById(id);
		Map productmap = new HashMap();
		productmap.clear();
		productmap.put("id", product.getId());
		productmap.put("image", product.getIcon());
		productmap.put("title", product.getTitle());
		productmap.put("price", product.getPrice());
		productmap.put("summary", product.getSummary());
		productmap.put("detail", product.getText());

		map.addAttribute("product", productmap);

		return "edit";
	}

	@RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
	public String editSubmit(@RequestParam int id, @RequestParam String title, @RequestParam String image,
			@RequestParam String detail, @RequestParam int price, @RequestParam String summary, HttpServletRequest req,
			ModelMap map) {

		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		ProductDao productdao = context.getBean(ProductDao.class);

		Product product = new Product();
		product.setId(id);
		product.setTitle(title);
		product.setPrice(price);
		product.setText(detail);
		product.setIcon(image);
		product.setSummary(summary);
		productdao.updateProduct(product);
		System.out.println("更新成功:" + product.getId());

		map.addAttribute("product", product);
		return "publicSubmit";
	}

	@RequestMapping(value = "/api/upload", method = RequestMethod.POST)
	public String fileUpload(@RequestParam MultipartFile file, HttpServletRequest req, ModelMap map) {

		// 判断文件是否为空
		if (!file.isEmpty()) {
			// 文件保存路径
			Date date = new Date();
			String webdir = req.getSession().getServletContext().getRealPath("/");

			File upload = new File(webdir + "upload");
			if (!upload.exists()) {
				upload.mkdirs();
			}

			String filePath = "/upload/" + file.getOriginalFilename();

			try {

				// 转存文件
				file.transferTo(new File(webdir + filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
			map.addAttribute("code", "200");
			map.addAttribute("message", "上传成功");
			map.addAttribute("result", filePath);
		} else {
			map.addAttribute("code", "400");
			map.addAttribute("message", "上传失败");
		}
		return "defaultViews";
	}
}
